<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="价格区间始" prop="changePriceStart">
        <el-input v-model="dataForm.changePriceStart" placeholder="价格区间始"></el-input>
      </el-form-item>
      <el-form-item label="价格区间止" prop="changePriceEnd">
        <el-input v-model="dataForm.changePriceEnd" placeholder="价格区间止"></el-input>
      </el-form-item>
      <el-form-item label="加价百分比" prop="changePricePercent">
        <el-input v-model="dataForm.changePricePercent" placeholder="加价百分比"></el-input>
      </el-form-item>
      <el-form-item label="加价额" prop="changePriceAmount">
        <el-input v-model="dataForm.changePriceAmount" placeholder="加价额"></el-input>
      </el-form-item>
      <el-form-item label="最高价" prop="highestPrice">
        <el-input v-model="dataForm.highestPrice" placeholder="最高价"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        dtId: '',
        hotelId: '',
        roomId: '',
        changePriceType: '',
        changePriceStart: '',
        changePriceEnd: '',
        changePricePercent: '',
        changePriceAmount: '',
        highestPrice: '',
        fixedPrice: ''
      },
      dataRule: {
        changePriceStart: [
          { required: true, message: '价格区间始不能为空', trigger: 'blur' }
        ],
        changePriceEnd: [
          { required: true, message: '价格区间止不能为空', trigger: 'blur' }
        ],
        changePricePercent: [
          { required: true, message: '加价百分比不能为空', trigger: 'blur' }
        ],
        changePriceAmount: [
          { required: true, message: '加价额不能为空', trigger: 'blur' }
        ],
        highestPrice: [
          { required: true, message: '最高价不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id,hotelId,roomId) {
      this.dataForm.id = id || 0
      this.dataForm.hotelId = hotelId
      this.dataForm.roomId = roomId
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/op/roomprice/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.hotelId = data.roomPrice.hotelId
              this.dataForm.roomId = data.roomPrice.roomId
              this.dataForm.changePriceStart = data.roomPrice.changePriceStart
              this.dataForm.changePriceEnd = data.roomPrice.changePriceEnd
              this.dataForm.changePricePercent = data.roomPrice.changePricePercent
              this.dataForm.changePriceAmount = data.roomPrice.changePriceAmount
              this.dataForm.highestPrice = data.roomPrice.highestPrice
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/op/roomprice/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'dtId': this.dataForm.dtId,
              'hotelId': this.dataForm.hotelId,
              'roomId': this.dataForm.roomId,
              'changePriceType': this.dataForm.changePriceType,
              'changePriceStart': this.dataForm.changePriceStart,
              'changePriceEnd': this.dataForm.changePriceEnd,
              'changePricePercent': this.dataForm.changePricePercent,
              'changePriceAmount': this.dataForm.changePriceAmount,
              'highestPrice': this.dataForm.highestPrice,
              'fixedPrice': this.dataForm.fixedPrice
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
